package com.cykj.service.Impl;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.AdminMapper;
import com.cykj.mapper.ConsultantMapper;
import com.cykj.pojo.PsAdmin;
import com.cykj.service.AdminService;
import com.cykj.util.ImageCodeUtils;
import com.cykj.util.JwtUtils;
import com.cykj.util.MD5Util;
import com.cykj.vo.AdminVO;
import com.cykj.vo.QueryAdminRoleOrConsultantVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-01-16 22:26
 * @Description: 管理员表service层
 * @Version: 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResponseDto login(String acc, String pwd, String code) {
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils();
        String code1 = imageCodeUtils.getCode();
        String encrypt = MD5Util.encrypt(pwd);
        PsAdmin admin = adminMapper.login(acc, encrypt,code);
        if(admin != null){
            final HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("adminId",admin.getAdminId());
            hashMap.put("adminRole",admin.getAdminRole());
            hashMap.put("adminName",admin.getAdminName());

            //生成token
             String token = JwtUtils.generateJwt(hashMap);
             return ResponseDto.success(token);
        }
        return ResponseDto.error("账号或密码错误！");
    }

    /**
     * 获取管理员表所有数据
     *
     * @return
     */
    @Override
    public ResponseDto getAll(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsAdmin> admins = adminMapper.getAll();
        PageInfo<PsAdmin> pageInfo = new PageInfo<>(admins);
        return ResponseDto.success(pageInfo);
    }

    /**
     * 获取管理员表 咨询师表 角色表
     *
     * @param vo
     * @return
     */
    @Override
    public ResponseDto getAdminOrConsultant(QueryAdminRoleOrConsultantVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<PsAdmin> admins = adminMapper.getAdminRoleConsultant(vo);
        PageInfo<PsAdmin> pageInfo = new PageInfo<>(admins);
        return ResponseDto.success(pageInfo);
    }

    /**
     * 修改管理员状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseDto updateAdminStatus(Integer id, Integer status) {
        int result = adminMapper.updateAdminStatus(id, status);
        if (result > 0) {
            return ResponseDto.success("修改成功");
        }
        return ResponseDto.error("修改失败");
    }

    /**
     * 后台管理员重置密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    @Override
    public ResponseDto resetAdminPassword(Integer id, String newPassword) {
        int i = adminMapper.resetAdminPassword(id, newPassword);
        if (i > 0) {
            return ResponseDto.success("密码重置成功!");
        }
        return ResponseDto.error("密码重置失败");
    }

    /**
     * 后台管理员列表软删除
     *
     * @param id
     * @param
     * @return
     */
    @Override
    public ResponseDto updateAdminDelete(Integer id) {
        int i = adminMapper.adminDelete(id);
        if (i > 0) {
            return ResponseDto.success("软删除成功");
        }
        return ResponseDto.error("软删除失败");
    }

    @Autowired
    private ConsultantMapper consultantMapper;

    /**
     * 新增管理员插入
     * @param vo
     * @return
     */
    @Override
    public ResponseDto addAdminOrConsultant(QueryAdminRoleOrConsultantVo vo) {
         if(adminMapper.getAdminAccount(vo).isEmpty()){
             if (vo.getRoleId() == 1) {
                 int res = adminMapper.insertAdmin(vo);
                 if (res > 0) {
                     return ResponseDto.success("管理员数据插入成功！");
                 } else {
                     return ResponseDto.error("管理员数据插入失败");
                 }
             } else {
                 int res = adminMapper.insertAdmin(vo);
                 if (res > 0) {
                     int i = consultantMapper.addConsultantMapper(vo);
                     if (i > 0) {
                         return ResponseDto.success("咨询师员数据插入成功");
                     }else {
                         return ResponseDto.success("管理员数据插入失败！");
                     }
                 } else {
                     return ResponseDto.error("管理员数据插入失败");
                 }
             }
         } else {
             return ResponseDto.success("账号已存在");
         }
    }
}

